//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "GJRGARCHModel.h"
using namespace Cephei::QL::Models::Equity;
#include <gen/QL/Processes/GJRGARCHProcess.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/CalibratedModel.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (Cephei::QL::Processes::IGJRGARCHProcess^ process) : CCalibratedModel(CGJRGARCHModel::typeid)
{
    CGJRGARCHProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phGJRGARCHModel = NULL;
#endif
        _Cprocess = safe_cast<CGJRGARCHProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GJRGARCHProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GJRGARCHProcess>&> (_Cprocess->GetShared ()); 
        _ppGJRGARCHModel = new boost::shared_ptr<QuantLib::GJRGARCHModel> (new QuantLib::GJRGARCHModel ( _process ));
        SetCalibratedModel (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (boost::shared_ptr<QuantLib::GJRGARCHModel>& childNative, Object^ owner) : CCalibratedModel(CGJRGARCHModel::typeid)
{
#ifdef HANDLE
	_phGJRGARCHModel = NULL;
#endif
	_ppGJRGARCHModel = &childNative;
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
}
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (QuantLib::GJRGARCHModel& childNative, Object^ owner) : CCalibratedModel(CGJRGARCHModel::typeid)
{
#ifdef HANDLE
	_phGJRGARCHModel = NULL;
#endif
	_ppGJRGARCHModel = new boost::shared_ptr<QuantLib::GJRGARCHModel> (&childNative);
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
    _GJRGARCHModelOwner = owner;
    _CalibratedModelOwner = owner;
}

Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (CGJRGARCHModel^ copy) : CCalibratedModel(CGJRGARCHModel::typeid)
{
#ifdef HANDLE
	_phGJRGARCHModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGJRGARCHModel = new boost::shared_ptr<QuantLib::GJRGARCHModel> (copy->GetShared());
        _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
    }
}
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (System::Type^ t) : CCalibratedModel(CGJRGARCHModel::typeid)
{
#ifdef HANDLE
	_phGJRGARCHModel = NULL;
#endif
	if (!t->IsSubclassOf(CGJRGARCHModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (QuantLib::Handle<QuantLib::GJRGARCHModel>& childNative, Object^ owner)  : CCalibratedModel(CGJRGARCHModel::typeid)
{
	_phGJRGARCHModel = &childNative;
	_ppGJRGARCHModel = &static_cast<boost::shared_ptr<QuantLib::GJRGARCHModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
    _GJRGARCHModelOwner = owner;
}
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (QuantLib::Handle<QuantLib::GJRGARCHModel> childNative)  : CCalibratedModel(CGJRGARCHModel::typeid)
{
	_phGJRGARCHModel = &childNative;
	_ppGJRGARCHModel = &static_cast<boost::shared_ptr<QuantLib::GJRGARCHModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Equity::CGJRGARCHModel::CGJRGARCHModel (QuantLib::GJRGARCHModel childNative)  : CCalibratedModel(CGJRGARCHModel::typeid)
{
#ifdef HANDLE
	_phGJRGARCHModel = NULL;
#endif
	_ppGJRGARCHModel = new boost::shared_ptr<QuantLib::GJRGARCHModel> (new QuantLib::GJRGARCHModel (childNative));
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppGJRGARCHModel));
}
#endif

Cephei::QL::Models::Equity::CGJRGARCHModel::~CGJRGARCHModel ()
{
    if (_ppGJRGARCHModel != NULL)
    {
	    delete _ppGJRGARCHModel;
        _ppGJRGARCHModel = NULL;
    }
}
Cephei::QL::Models::Equity::CGJRGARCHModel::!CGJRGARCHModel ()
{
    if (_ppGJRGARCHModel != NULL)
    {
	    delete _ppGJRGARCHModel;
    }
}
QuantLib::GJRGARCHModel& Cephei::QL::Models::Equity::CGJRGARCHModel::GetReference ()
{
    if (_ppGJRGARCHModel == NULL) throw gcnew NativeNullException ();
	return **_ppGJRGARCHModel;
}
boost::shared_ptr<QuantLib::GJRGARCHModel>& Cephei::QL::Models::Equity::CGJRGARCHModel::GetShared ()
{
    if (_ppGJRGARCHModel == NULL) throw gcnew NativeNullException ();
	return *_ppGJRGARCHModel;
}
QuantLib::GJRGARCHModel* Cephei::QL::Models::Equity::CGJRGARCHModel::GetPointer ()
{
    if (_ppGJRGARCHModel == NULL) throw gcnew NativeNullException ();
	return &**_ppGJRGARCHModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GJRGARCHModel>& Cephei::QL::Models::Equity::CGJRGARCHModel::GetHandle ()
{
	if (_phGJRGARCHModel == NULL)
	{
		_phGJRGARCHModel = new Handle<QuantLib::GJRGARCHModel> (*_ppGJRGARCHModel);
	}
	return *_phGJRGARCHModel;
}
#endif
bool Cephei::QL::Models::Equity::CGJRGARCHModel::HasNative () 
{
	return (_ppGJRGARCHModel != NULL);
}

Double Cephei::QL::Models::Equity::CGJRGARCHModel::Alpha::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->alpha ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CGJRGARCHModel::Beta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->beta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CGJRGARCHModel::Gamma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->gamma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CGJRGARCHModel::Lambda::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->lambda ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CGJRGARCHModel::Omega::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->omega ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IGJRGARCHProcess^ Cephei::QL::Models::Equity::CGJRGARCHModel::Process::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::GJRGARCHProcess> _rv = (boost::shared_ptr<QuantLib::GJRGARCHProcess>)(*_ppGJRGARCHModel)->process ( );   
        Cephei::QL::Processes::CGJRGARCHProcess^ _nrv = gcnew Cephei::QL::Processes::CGJRGARCHProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CGJRGARCHModel::V0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGJRGARCHModel)->v0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Equity::IGJRGARCHModel^ Cephei::QL::Models::Equity::CGJRGARCHModel_Factory::Create (Cephei::QL::Processes::IGJRGARCHProcess^ process)
{
    return gcnew CGJRGARCHModel ( process);
}
